import { setMenu } from '../utils/utils';
import { getMenu } from '../services/API';
export default {
  namespace: 'dashboard',
  state: {
    menuData: []
  },
  effects: {
    *doGetMenu({ payload }, { call, put }) {
      const res = yield call(getMenu, payload);
      // console.log(res, '菜单列表')
      if (res.code === 200) {
        setMenu(JSON.stringify(res.data));
        yield put({
          type: 'save',
          menuData: res.data
        });
      }
    }
  },

  reducers: {
    //设置菜单状态
    save(state, action) {
      return { ...state, ...action.payload };
    }
  },
  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(({ pathname }) => {
        if (pathname === '/index') {
          dispatch({
            type: 'doGetMenu'
          });
        }
      });
    }
  }
};
